

declare @tabela varchar(100)

set nocount on

create table #groups (Group_name VARCHAR(50),Group_id SMALLINT)

INSERT INTO #Groups
EXEC('sp_helpgroup')

declare cur_tabelas scroll cursor for
select name from sysobjects where type in ('U', 'V', 'P') and uid=1
order by name

open cur_tabelas
fetch next from cur_tabelas into @tabela

while @@fetch_status <> -1
begin
IF EXISTS(SELECT * FROM #Groups WHERE Group_Name='public')
exec('grant all on ' + @tabela + ' to public')
IF EXISTS(SELECT * FROM #Groups WHERE Group_Name='dbas')
exec('grant all on ' + @tabela + ' to dbas')
fetch next from cur_tabelas into @tabela 
end

close cur_tabelas
deallocate cur_tabelas 
DROP TABLE #Groups
set nocount off

